// [brand,series,model]
export const getCarModels = (list) => {
  const newArray = [];
  const newArray2 = [];
  list.forEach((item) => {
    const repeat = newArray.find((res) => res.label === item.brand);
    if (!repeat) {
      newArray.push({
        value: item.brand,
        label: item.brand,
        children: [item],
      });
    } else {
      repeat.children.push(item);
    }
  });

  newArray.forEach((item) => {
    item.children.forEach((i) => {
      const repeat = newArray2.find((res) => res.label === i.series);
      if (!repeat) {
        newArray2.push({
          value: i.series,
          label: i.series,
          brand: i.brand,
          children: [
            {
              value: i.id,
              label: i.model,
            },
          ],
        });
      } else {
        repeat.children.push({
          value: i.id,
          label: i.model,
        });
      }
    });
  });
  newArray.forEach((item) => {
    item.children.length = 0;
    newArray2.forEach((i) => {
      if (item.label == i.brand) {
        item.children.push({
          value: i.value,
          label: i.label,
          children: i.children,
        });
      }
    });
  });
  return newArray;
};
// [country,city,harbor]
export const getPortModels = (list) => {
  const newArray = [];
  const newArray2 = [];
  list.forEach((item) => {
    const repeat = newArray.find((res) => res.label === item.country);
    if (!repeat) {
      newArray.push({
        value: item.country,
        label: item.country,
        children: [item],
      });
    } else {
      repeat.children.push(item);
    }
  });

  newArray.forEach((item) => {
    item.children.forEach((i) => {
      const repeat = newArray2.find((res) => res.label === i.city);
      if (!repeat) {
        newArray2.push({
          value: i.city,
          label: i.city,
          country: i.country,
          children: [
            {
              value: i.id,
              label: i.harbor,
            },
          ],
        });
      } else {
        repeat.children.push({
          value: i.id,
          label: i.harbor,
        });
      }
    });
  });
  newArray.forEach((item) => {
    item.children.length = 0;
    newArray2.forEach((i) => {
      if (item.label == i.country) {
        item.children.push({
          value: i.value,
          label: i.label,
          children: i.children,
        });
      }
    });
  });
  return newArray;
};
